﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// InspectViolation操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InspectViolationsController : ControllerBase
    {
        private readonly InspectViolationApp _app;

        //获取详情
        [HttpGet]
        public Response<InspectViolation> Get(string id)
        {
            var result = new Response<InspectViolation>();
            try
            {
                result.Result = _app.Get(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        //添加
        [HttpPost]
        public Response Add(InspectViolation obj)
        {
            var result = new Response();
            try
            {
                _app.Add(obj);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        //修改
        [HttpPost]
        public Response Update(InspectViolation obj)
        {
            var result = new Response();
            try
            {
                _app.Update(obj);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取违规详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Response GetDetail(string id)
        {
            var result = new Response<InspectViolationList>();
            try
            {
                result.Result = _app.GetDetail(id);
            }
            catch (Exception e)
            {
                result.Code = 500;
                result.Message = e.InnerException?.Message ?? e.Message;
            }
            return result;
        }

        //public Response GetFileFromWebApi([FromQuery]QueryInspectViolationListReq request)
        //{
        //    var result = new Response();
        //    try
        //    {
        //        var path= _app.ExportExcel(request);
        //        FileInfo file = new FileInfo(path);
        //    }
        //    catch (Exception e)
        //    {
        //        result.Code = 500;
        //        result.Message = e.InnerException?.Message ?? e.Message;
        //    }
        //    return result;
        //}

        [HttpGet]
        public Response GetFileFromWebApi([FromQuery]QueryInspectViolationListReq request)
        {
            var outData = new Response<string>();
            try
            {
                var FilePath = _app.ExportExcel(request);
                outData.Result = FilePath;
            }
            catch(Exception e)
            {
                outData.Code = 500;
                outData.Message = e.InnerException?.Message ?? e.Message;
            }   
            return outData;
        }
        /// <summary>
        /// 加载列表
        /// </summary>
        [HttpGet]
        public TableData Load([FromQuery]QueryInspectViolationListReq request)
        {
            return _app.GetData(request);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        [HttpPost]
        public Response Delete([FromBody]string[] ids)
        {
            var result = new Response();
            try
            {
                _app.Delete(ids);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        [HttpGet]
        public Response GetAllPlans()
        {
            var result = new Response<List<InspectPlan>>();
            try
            {
                result.Result = _app.GetAllPlans();

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取报表的详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public TableData GetReportDetailList([FromQuery]QueryInspectViolationListReq request)
        {
            return _app.GetReportDetailList(request);
        }

        public InspectViolationsController(InspectViolationApp app)
        {
            _app = app;
        }
    }
}
